package com.youku.plugins.programListV4.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.PluginEventDispather;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.ConductorEvent;
   import flash.events.MouseEvent;
   import com.youku.plugins.programListV4.view.event.ProgramListEvent;
   import flash.events.Event;
   import com.youku.interfaces.IProgramData;
   import com.youku.utils.ReportUtil;
   import com.youku.events.PluginEvent;
   import com.youku.conductor.ConductorState;
   import com.youku.data.PlayerConstant;
   
   public class ProgramListMediator extends Object
   {
      
      public function ProgramListMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMC = param1;
         this._playerProxy = param2;
         this._ProgramListEventManager = PluginEventDispather.getInstance();
         this._programList = new ProgramList();
         this.addEvents();
      }
      
      private var _mainMC:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _ProgramListEventManager:PluginEventDispather;
      
      private var _isShowing:Boolean = false;
      
      private var _programList:ProgramList;
      
      private function addEvents() : void {
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.PROGRAMLIST_SHOW,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.BEFORE_SHOW_PANELS,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetadataUpdated);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_END_CARD,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(MouseEvent.MOUSE_WHEEL,this.onMouseWheel);
         this._ProgramListEventManager.addEventListener(ProgramListEvent.PROGRAM_LIST_CLICK,this.onProgramClick);
         this.programList.addEventListener(ProgramListEvent.CLOSE_BTN_CLICK,this.onCloseBtnClick);
      }
      
      private function onMouseWheel(param1:MouseEvent) : void {
         if(!this._isShowing)
         {
            return;
         }
         if(param1.delta > 0)
         {
            this._programList.wheelUp();
         }
         else if(param1.delta < 0)
         {
            this._programList.wheelDown();
         }
         
      }
      
      private function onCloseBtnClick(param1:Event) : void {
         this._playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.PROGRAMLIST_SHOW));
      }
      
      private function onProgramClick(param1:ProgramListEvent) : void {
         var _loc2_:IProgramData = param1.data as IProgramData;
         ReportUtil.sendUserActionLog(ReportUtil.OFFICIAL_LIST_SINGLE_OFFICIAL_CLICK);
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PROGRAMLIST_VIDEO_CLICK,_loc2_));
      }
      
      private function onMetadataUpdated(param1:ConductorEvent) : void {
         var _loc7_:IProgramData = null;
         var _loc8_:String = null;
         this.programList.reset();
         var _loc2_:Array = this._playerProxy.playerData.videoListPrograms;
         if(_loc2_ == null)
         {
            return;
         }
         var _loc3_:* = "VCR";
         if(this._playerProxy.playerData.playmode == "folder")
         {
            _loc3_ = "VCR";
         }
         else if(this._playerProxy.playerData.playmode == "show")
         {
            if(this._playerProxy.playerData.videoCt == "d")
            {
               _loc3_ = "TVS";
            }
            else
            {
               _loc3_ = "VCR";
            }
         }
         
         var _loc4_:String = this._playerProxy.rootGetData.rootFolderID;
         var _loc5_:String = this._playerProxy.rootGetData.rootOb;
         var _loc6_:* = 0;
         while(_loc6_ < _loc2_.length)
         {
            _loc7_ = _loc2_[_loc6_] as IProgramData;
            _loc7_.type = _loc3_;
            _loc8_ = "http://v.youku.com/v_show/id_";
            _loc8_ = _loc8_ + _loc7_.videoID;
            _loc8_ = _loc8_ + ".html";
            if(_loc7_.type == "VCR" && !(_loc4_ == null))
            {
               _loc8_ = _loc8_ + ("?f=" + _loc4_);
               if(_loc5_ == "0")
               {
                  _loc8_ = _loc8_ + "&o=0";
               }
            }
            _loc7_.URL = _loc8_;
            _loc6_++;
         }
         this.programList.init(this._playerProxy.playerData.videoListPrograms,this._playerProxy.playerData.videoIdEncode);
      }
      
      private function onStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         switch(_loc2_)
         {
            case ConductorState.INITIALIZE:
               this.hide();
               break;
         }
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:String = null;
         switch(param1.type)
         {
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
               this.programList.changeSize(param1.data.width,param1.data.height);
               break;
            case PlayerControlEvent.PROGRAMLIST_SHOW:
               if(this._isShowing)
               {
                  this.hide();
               }
               else
               {
                  this.show();
               }
               break;
            case PlayerControlEvent.SHOW_END_CARD:
               this.hide();
               break;
            case PlayerControlEvent.BEFORE_SHOW_PANELS:
               _loc2_ = param1.data.panel;
               if(_loc2_ != PlayerConstant.PANEL_PROGRAM_LIST)
               {
                  this.hide();
               }
               break;
         }
      }
      
      private function show() : void {
         this._mainMC.addChild(this.programList);
         this.programList.show();
         this._isShowing = true;
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
            {
               "type":"programList",
               "state":true
            }));
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PROGRAMLIST_SHOW));
      }
      
      private function hide() : void {
         if(this._isShowing)
         {
            if(this._mainMC.contains(this.programList))
            {
               this._mainMC.removeChild(this.programList);
            }
            this._isShowing = false;
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
               {
                  "type":"programList",
                  "state":false
               }));
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PROGRAMLIST_HIDE));
         }
      }
      
      private function get programList() : ProgramList {
         return this._programList as ProgramList;
      }
   }
}
